﻿using System.ComponentModel.DataAnnotations;
using Tester.DL645.Instructions;
using Tester.DL645.Responses;

namespace Tester.DL645.Devices
{
    /// <summary>
    /// 默认设备
    /// </summary>
    public class DefaultDevice
    {

        /// <summary>
        /// 
        /// </summary>
        private DefaultDevice()
        {

        }
        /// <summary>
        /// 指令编译
        /// </summary>
        [Required]
        public InstructionBuilder InstructionBuilder { get; set; }

        /// <summary>
        /// 响应编译
        /// </summary>
        [Required]
        public ResponseBuilder ResponseBuilder { get; set; }
        /// <summary>
        ///  默认设备
        /// </summary>
        /// <param name="address"></param>
        /// <param name="password"></param>
        /// <param name="auth"></param>
        public DefaultDevice([Required] string address, string password = "0000", string auth = "02")
            : this(new InstructionBuilder(address, password, auth))
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="instructionBuilder"></param>
        public DefaultDevice(InstructionBuilder instructionBuilder)
            :this(instructionBuilder,new ResponseBuilder())
        {
                
        }
        /// <summary>
        /// 默认
        /// </summary>
        /// <param name="instructionBuilder"></param>
        /// <param name="responseBuilder"></param>
        public DefaultDevice(InstructionBuilder instructionBuilder, ResponseBuilder responseBuilder)
        {
            this.InstructionBuilder = instructionBuilder;
            this.ResponseBuilder = responseBuilder;
            this.ReponseReceived += DefaultDevice_ReponseReceived;
        }

        private void DefaultDevice_ReponseReceived(object? sender, ReponseEventArgs e)
        {
             ///打日志
        }

        /// <summary>
        /// 收到响应
        /// </summary>
        public event EventHandler<Responses.ReponseEventArgs> ReponseReceived;

        /// <summary>
        /// 生成对于的响应
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="responseType"></param>
        /// <returns></returns>
        public AbstractReponse CreateResponse(byte[] buffer, Core.ResponseType responseType)
        {
            try
            {
                var result = this.ResponseBuilder.CreateResponse(buffer, responseType);

                return result;
            } catch (Exception ex)
            {
                var ii = 0;
            }
            return new ErrorReponse();

        }

        /// <summary>
        /// 备用
        /// </summary>
        /// <param name="instructionType"></param>
        /// <returns></returns>
        public void Input(byte[] buffer, Core.ResponseType responseType)
        {
            var result= this.ResponseBuilder.CreateResponse(buffer, responseType);

            if (result.ResponseType!= Core.ResponseType.No)
            {
                this.ReponseReceived.Invoke(this, new ReponseEventArgs(responseType, result));
            }
        }

        /// <summary>
        /// 生成指令
        /// </summary>
        /// <param name="instruction"></param>
        /// <returns></returns>
        public AbstractInstruction CreateInstruction(Core.InstructionType instructionType)
        {
            return this.InstructionBuilder.CreateInstruction(instructionType);
        }
    }
}
